//
//  FindPeopleViewController.m
//  MyProject
//
//  Created by 石小强 on 2018/4/14.
//  Copyright © 2018年 yanjiaming. All rights reserved.
//

#import "FindPeopleViewController.h"
#import "FindPeopleFlowLayout.h"
#import "CompanyInfoCertificateVC.h"
#import "FindPeopleCell.h"
#import "FindPeopleLastCell.h"
#import "IncumbentModel.h"
#import <Hyphenate/Hyphenate.h>
#import "NSUDManager.h"
#import "WorkmateMainVC.h"
#import "CompanyInfoCertificateVC.h"
#import "MoreFindPeopleListViewController.h"

@interface FindPeopleViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, FindPeopleFlowLayoutDelegate>

@property (nonatomic,strong)UICollectionView * myCollectionView;
@property (nonatomic, strong) NSMutableArray *dataArray;

@end

@implementation FindPeopleViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBarHidden = YES;
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    if ([Tool isIphoneX]) {
        self.statusViewHeight.constant = 44;
    } else {
        self.statusViewHeight.constant = 20;
    }
    
//    self.backImgV.image = [UIImage imageNamed:@"ic_service_hand"];
    
//    CGFloat itemW = kScreenWidth - 54;
    CGFloat itemW = kScreenWidth - 20;
    CGFloat itemH = (kScreenWidth - 34) * 409 / 321;
    
    FindPeopleFlowLayout *layout = [[FindPeopleFlowLayout alloc] init];
    layout.itemSize = CGSizeMake(itemW, itemH);
    layout.scale = 0.9;
    layout.delegate = self;
    
    CGFloat navH = [Tool isIphoneX] ? 24 : 0;
    _myCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 150 + navH, kScreenWidth, 500) collectionViewLayout:layout];
    _myCollectionView.delegate = self;
    _myCollectionView.dataSource = self;
    _myCollectionView.backgroundColor = [UIColor clearColor];
    _myCollectionView.showsHorizontalScrollIndicator = NO;
    [self.view addSubview:_myCollectionView];
    
    [_myCollectionView registerNib:[UINib nibWithNibName:NSStringFromClass([FindPeopleCell class]) bundle:nil] forCellWithReuseIdentifier:@"FindPeopleCell"];
    [_myCollectionView registerNib:[UINib nibWithNibName:NSStringFromClass([FindPeopleLastCell class]) bundle:nil] forCellWithReuseIdentifier:@"FindPeopleLastCell"];
    
//    NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:@"1/20"];
//    [str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"PingFang SC" size:50] range:NSMakeRange(0, 1)];
//    self.pageL.attributedText = str;
    
    [self loadData];
}

- (void)loadData {
    WS(weakSelf)
    [self showProgressWithView:self.view animated:YES];
    [SendRequest getMatchingIncumbentsWithPageNo:@"1" result:^(NSDictionary *result, NSError *error) {
        [weakSelf hideProgress:self.view animated:YES];
        result = [CommonUtils decryptDicWithResult:result];
        if([result[@"result"] integerValue] == 1) {
            SaintiLog(@"谁能帮我 = %@", [result objectForKey:@"data"]);
            NSArray *datas = [[result objectForKey:@"data"] objectForKey:@"list"];
            NSArray *dataIncuments = [IncumbentModel mj_objectArrayWithKeyValuesArray:datas];
            
            [weakSelf.dataArray removeAllObjects];
            [weakSelf.dataArray addObjectsFromArray:dataIncuments];
            
            IncumbentModel *last = [[IncumbentModel alloc] init];
            [weakSelf.dataArray addObject:last];
            
            if (dataIncuments.count > 0) {
                IncumbentModel *first = [weakSelf.dataArray objectAtIndex:0];
                [weakSelf.backImgV sd_setImageWithURL:ImageURLWithKey(first.headImg) placeholderImage:PlaceholderImage];
            } else {
                weakSelf.backImgV.image = PlaceholderImage;
            }
            
            
            NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"1/%zd", weakSelf.dataArray.count]];
            [str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"PingFang SC" size:50] range:NSMakeRange(0, 1)];
            weakSelf.pageL.attributedText = str;
            
            [weakSelf.myCollectionView reloadData];
        } else {
            weakSelf.backImgV.image = PlaceholderImage;
            [CommonUtils showToast:[result objectForKey:@"msg"]];
        }
    }];
}

#pragma mark - UICollectionViewDataSource

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == self.dataArray.count - 1) {
        FindPeopleLastCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"FindPeopleLastCell" forIndexPath:indexPath];
        cell.backgroundColor = [UIColor clearColor];
        return cell;
    } else {
        FindPeopleCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"FindPeopleCell" forIndexPath:indexPath];
        cell.backgroundColor = [UIColor clearColor];
        cell.model = [self.dataArray objectAtIndex:indexPath.row];
        return cell;
    }
}

#pragma mark - UICollectionViewDelegate

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"didSelectItemAtIndexPath.row = %zd", indexPath.row);
    
//    CompanyInfoCertificateVC *companyVC = [[CompanyInfoCertificateVC alloc] init];
//    [self.navigationController pushViewController:companyVC animated:YES];
//    return;
    
    if (indexPath.row == self.dataArray.count - 1) {
//        [CommonUtils showToast:@"跳转到搜索页---还不太懂"];
        MoreFindPeopleListViewController *vc = [[MoreFindPeopleListViewController alloc] init];
        vc.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:vc animated:YES];
    } else {
        IncumbentModel *model = [self.dataArray objectAtIndex:indexPath.row];
        WorkmateMainVC *vc = [[WorkmateMainVC alloc] init];
        vc.userId = model.userId;
        vc.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:vc animated:YES];
    }
}

#pragma mark - FindPeopleFlowLayoutDelegate

- (void)scrolledToTheCurrentItemAtIndex:(NSInteger)itemIndex {
    NSLog(@"scrolledToTheCurrentItemAtIndex = %zd", itemIndex);
    
    NSString *content = [NSString stringWithFormat:@"%zd/%zd", (itemIndex + 1), self.dataArray.count];
    
    NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:content];
    if (itemIndex < 9) {
        [str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"PingFang SC" size:50] range:NSMakeRange(0, 1)];
    } else if (itemIndex < 99) {
        [str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"PingFang SC" size:50] range:NSMakeRange(0, 2)];
    } else {
        [str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"PingFang SC" size:50] range:NSMakeRange(0, 3)];
    }
    
    self.pageL.attributedText = str;
    
    if (itemIndex == self.dataArray.count - 1) {
        self.backImgV.image = PlaceholderImage;
    } else {
        IncumbentModel *model = [self.dataArray objectAtIndex:itemIndex];
        [self.backImgV sd_setImageWithURL:ImageURLWithKey(model.headImg) placeholderImage:PlaceholderImage];
    }
}

- (IBAction)btnClick:(id)sender {
    CompanyInfoCertificateVC *vc = [[CompanyInfoCertificateVC alloc] init];
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
}


-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
    UserModel *user = [NSUDManager currentUser];
    if (user) {

        if ([[EMClient sharedClient] isLoggedIn]||[[EMClient sharedClient] isConnected]) {
            [[EMClient sharedClient] loginWithUsername:user.userId
                                              password:user.userId
                                            completion:^(NSString *aUsername, EMError *aError) {
                                                if (!aError) {
                                                    NSLog(@"登录成功");
                                                } else {
                                                    NSLog(@"登录失败");
                                                }
                                            }];
        }else{
            SaintiLog(@"------------------------历史登录成功");
        }
    }
}
-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
}
-(void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
}

- (NSMutableArray *)dataArray {
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

@end
